package com.ds.testspeed;

import java.util.ArrayList;
import java.util.LinkedList;

public class TestSpeed {

	public static void main(String[] args) {

		//数组和链表的增删遍历效率比较

		long start = 0;
		long end = 0;

		ArrayList<Integer> l1 = new ArrayList<>();
		LinkedList<Integer> l2 = new LinkedList<>();

		System.out.println("-----添加效率测试-----");

		System.out.println("向ArrayList中插入100000个元素...");
		start = System.currentTimeMillis();
		for(int i = 0; i < 100000; i++) {
			l1.add(0, i);
		}
		end = System.currentTimeMillis();
		System.out.println("添加完成，用时" + (end - start) + "毫秒");

		System.out.println("向LinkedList中插入100000个元素...");
		start = System.currentTimeMillis();
		for(int i = 0; i < 100000; i++) {
			l2.add(0, i);
		}
		end = System.currentTimeMillis();
		System.out.println("添加完成，用时" + (end - start) + "毫秒");

		System.out.println("-----遍历效率测试-----");

		System.out.println("对ArrayList执行100000次元素查找...");
		start = System.currentTimeMillis();
		for(int i = 0; i < 100000; i++) {
			l1.get(i);
		}
		end = System.currentTimeMillis();
		System.out.println("遍历完成，用时" + (end - start) + "毫秒");

		System.out.println("对LinkedList执行100000次元素查找...");
		start = System.currentTimeMillis();
		for(int i = 0; i < 100000; i++) {
			l2.get(i);
		}
		end = System.currentTimeMillis();
		System.out.println("遍历完成，用时" + (end - start) + "毫秒");

	}

}
